programming4us
           
 
 
Programming

jQuery 1.3 : Compact forms (part 1) - Placeholder text for fields

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/14/2010 3:45:08 PM
Some forms are much simpler than contact forms. In fact, many sites incorporate a single-field form on every single page: a search function for the site. The usual trappings of a form—field labels, submit buttons, and the text—are cumbersome for such a small, single-purpose part of the page. We can use jQuery to help slim down the form while retaining its functionalities, and even enhance its behavior to be much more usable than a full-page equivalent.

Placeholder text for fields

The<label> element for a form field is an essential component of accessible websites. Every field should be labeled so that screen readers and other assistive devices can identify which field is used, and for what purpose. Even in the HTML source, the label helps describe the field:

<form id="search" action="search/index.php" method="get">
<label for="search-text">search the site</label>
<input type="text" name="search-text" id="search-text" />
</form>

Without styling, we see the label right before the field:

While this doesn't take up much room, in some site layouts even this single line of text might be too much. We could hide the text with CSS, but this then provides the user with no way to know what the field is for. Instead, we'll use CSS to position the label on top of the field, only if JavaScript is available, by adding a class to the search form:

$(document).ready(function() {
var $search = $('#search').addClass('overlabel');
});

In a single line we're adding a class to the search form and storing the selector in a variable so that we can refer to it later. The stylesheet uses the overlabel class to style the label:

.overlabel {
position: relative;
}
.overlabel label {
position: absolute;
top: 6px;
left: 3px;
color: #999;
cursor: text;
}

Not only does the added class position the label properly, but it also grays out the text to distinguish it as a placeholder:

This is a nice effect, but it has a couple problems:

  1. 1. The label text obscures any text that the user enters into the text field.

  2. 2. The input can only be accessed now by tabbing into it. Since the label is covering the input, the user is prevented from clicking into it.

To avoid the first problem, we need to hide the label text when the field gets focus, and show it again when the focus is lost, as long as there is no user-entered text in the field.

Discussion about keyboard focus can be found in Chapter 3.


Hiding the label text on focus is simple enough:

$(document).ready(function() {
var $search = $('#search').addClass('overlabel');
var $searchInput = $search.find('input');
var $searchLabel = $search.find('label');
$searchInput
.focus(function() {
$searchLabel.hide();
})
.blur(function() {
if (this.value == '') {
$searchLabel.show();
}
});
});

The label is now neatly hidden when the user types text into the field:

The second problem is now quite easy to solve as well. We can both hide the label text and give the user access to the input by letting a click on the label trigger the event for the input: focus

$(document).ready(function() {
var $search = $('#search').addClass('overlabel');
var $searchInput = $search.find('input');
var $searchLabel = $search.find('label');
$searchInput
.focus(function() {
$searchLabel.hide();
})
.blur(function() {
if (this.value == '') {
$searchLabel.show();
}
});
$searchLabel.click(function() {
$searchInput.trigger('focus');
});

});

Finally, we need to handle the case in which text remains in the input when the page is refreshed&mdash;similar to what we had to do with the conditional inputs in the form validation section earlier in this chapter. If the input has a value, the label is hidden:

$(document).ready(function() {
var $search = $('#search').addClass('overlabel');
var $searchInput = $search.find('input');
var $searchLabel = $search.find('label');
if ($searchInput.val()) {
$searchLabel.hide();

}
$searchInput
.focus(function() {
$searchLabel.hide();
})
.blur(function() {
if (this.value == '') {
$searchLabel.show();
}
});
$searchLabel.click(function() {
$searchInput.trigger('focus');
});
});

One advantage of using the label rather than inserting a default value directly into the text input is that this technique can be adapted to any text field without having to worry about a potential conflict with a validation script.
Other -----------------
- The Art of SEO : Duplicate Content Issues (part 3)
- The Art of SEO : Duplicate Content Issues (part 2) - How Search Engines Identify Duplicate Content
- The Art of SEO : Duplicate Content Issues (part 1) - Consequences of Duplicate Content
- The Art of SEO : Content Optimization (part 2)
- The Art of SEO : Content Optimization (part 1)
- iPad SDK : New Graphics Functionality - Introducing Dudel (part 2)
- iPad SDK : New Graphics Functionality - Introducing Dudel (part 1)
- iPad SDK : New Graphics Functionality - Bezier Paths
- CSS for Mobile Browsers : Where to Insert the CSS (part 2) - Media queries
- CSS for Mobile Browsers : Where to Insert the CSS (part 1) - Media Filtering
- Developing an SEO-Friendly Website : Keyword Targeting (part 4)
- Developing an SEO-Friendly Website : Keyword Targeting (part 3)
- Developing an SEO-Friendly Website : Keyword Targeting (part 2)
- Developing an SEO-Friendly Website : Keyword Targeting (part 1) - Title Tags
- The Art of SEO : Optimization of Domain Names/URLs
- Cloud Security and Privacy : Regulatory/External Compliance (part 2)
- Cloud Security and Privacy : Regulatory/External Compliance (part 1)
- Developing an SEO-Friendly Website : Root Domains, Subdomains, and Microsites (part 2)
- Developing an SEO-Friendly Website : Root Domains, Subdomains, and Microsites (part 1)
- Parallel Programming with Microsoft .Net : Parallel Aggregation - An Example
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us